<?php

class Model_Product extends Joob_Db_Table	{
	
	protected $key = "id";
	
	protected $db = "products";

	public function getInfo()	{
		return array('id'=>$this->id, 'name'=>$this->name, 'image'=>$this->image,
				'description'=>$this->description
		);
	}
	
	public function editDetail()	{
		return $this->edit('name, description');
	}
	
	public function editImage()	{
		return $this->edit('image');
	}
	
	public function getFeaturedProduct($limit=16)	{
		$rs = $this->search('is_featured=1', 'id, name, image, description', $limit);
		$result = array();
		foreach ($rs as $row) {
			if ($row->description == null)	{
				$row->description = '';
			}
			$result[] = $row;
		}
		return $result;
	}
	
	public function addNew()	{
		$this->since = time();
		return $this->insert('name, category_id, description, is_new, is_featured, since');
	}
	
	public function updateProduct()	{
		return $this->edit('name, category_id, description, is_new, is_featured');
	}
	
	public static function browse()	{
		$model = new Model_Product();
		$rs = $model->search('', 'id, name, category_id, is_featured, is_new');
		$result = array();
		foreach ($rs as $row)	{
			$cid = $row->category_id;
			$category = new Model_Category();
			$category->id = $cid;
			$category->connect();
			$row->category = array();
			$row->category['name'] = $category->name;
			$row->category['id'] = $category->id;
			unset($row->category_id);
			$result[] = $row;
		}
		return $result;
	}
	public static function searchProducts($key,$page=0,$page_size=16){
		$key = str_replace(' ','% %',$key);
		$product = new Model_Product();
		return $product->search(" status=1 and (name like '%".$key."%')", 'id, name, image', $page*$page_size.','.$page_size);
	}
}

?>